import Vue from 'vue';
import Router from 'vue-router';

Vue.use(Router);

//获取原型对象上的push函数
const originalPush = Router.prototype.push;
//修改原型对象中的push方法
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
};


const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/',
    component: () => import('../components/common/Home.vue'),
    meta: { title: '自述文件' },
    children: [
      {
        path: '/dashboard',
        component: () => import('../components/page/Dashboard.vue'),
        meta: { title: '系统首页' }
      },
      {
        path: '/userManage',
        component: () => import('../components/page/BaseTable.vue'),
        meta: { title: '用户管理' }
      },
      {
        path: '/collegeManage',
        component:() => import('../components/page/CollegeManage.vue'),
        meta: { title: '学院管理' }
      },
      {
        path: '/deptManage',
        component:() => import('../components/page/DeptManage.vue'),
        meta: { title: '党支部管理' }
      },
      {
        path: '/managerManage',
        component:() => import('../components/page/ManagerManage.vue'),
        meta: { title: '管理员管理管理' }
      },
      {
        path: '/paperCreate',
        component: () => import('../components/page/paper/PaperCreate.vue'),
        meta: { title: '试卷创编' }
      },
      {
        path: '/RandomPaper',
        component: () => import('../components/page/paper/RandomPaper.vue'),
        meta: { title: '随机试卷' }
      },
      {
        path: '/examList',
        component: () => import('../components/page/exam/ExamList.vue'),
        meta: { title: '考试列表' }
      },
      {
        path: '/correctList',
        component: () => import('../components/page/exam/CorrectList.vue'),
        meta: { title: '批改列表' }
      },
      {
        path: '/correctPaper',
        component: () => import('../components/page/answer/CorrectPaper.vue'),
        meta: { title: '试卷批改' }
      },
      {
        path: '/examInfo',
        component: () => import('../components/page/exam/ExamInfo.vue'),
        meta: { title: '考试详情'},
        hidden: true
      },
      {
        path: '/examInfoEnd',
        component: () => import('../components/page/exam/ExamInfoEnd.vue'),
        meta: { title: '考试详情'},
        hidden: true
      },
      {
        path: '/questionList',
        component: () => import('../components/page/question/QuestionList.vue'),
        meta: { title: '题目列表'},

      },
      {
        path: '/singleCreate',
        component: () => import('../components/page/question/SingleCreate.vue'),
        meta: { title: '单选题创编' }
      },
      {
        path: '/multipleCreate',
        component: () => import('../components/page/question/MultipleCreate.vue'),
        meta: { title: '多选题创编' }
      },
      {
        path: '/judgeCreate',
        component: () => import('../components/page/question/JudgeCreate.vue'),
        meta: { title: '判断题创编' }
      },
      {
        path: '/fillCreate',
        component: () => import('../components/page/question/FillCreate.vue'),
        meta: { title: '填空题创编' }
      },
      {
        path: '/shotAnswerCreate',
        component: () => import('../components/page/question/ShotAnswerCreate.vue'),
        meta: { title: '简答题创编' }
      },
      {
        // 富文本编辑器组件
        path: '/editor',
        component: () => import('../components/page/VueEditor.vue'),
        meta: { title: '考试管理' }
      },
      {
        // 图片上传组件
        path: '/upload',
        component: () => import('../components/page/Upload.vue'),
        meta: { title: '文件上传' }
      },

      {
        path: '/404',
        component: () => import('../components/page/404.vue'),
        meta: { title: '404' }
      }
    ]
  },
  {
    path: '/login',
    component: () => import('../components/page/Login.vue'),
    meta: { title: '登录' }
  },
  {
    path: '*',
    redirect: '/404'
  }
];

const router = new Router({
  routes
});

router.beforeEach((to, from, next) => {
  document.title = `${to.meta.title} | vue-manage-system`;
  const account = localStorage.getItem('account');
  console.log(account);
  if (!account && to.path !== '/login' && next.path !== '/login') {
    next('/login');
  } else {
    next();
  }
});

export default router;
